import * as dayjs from "dayjs";
import { isNil } from "lodash";

// 代码 ---> 值  {label: string , value: string | number }
export function transFormatter(val: number | string, data: any) {
    for (let index = 0; index < data.length; index++) {
        const element = data[index];
        if (element.value == val) {
            return element.label;
        }
    }
    return val;
}
//格式化日期时间
export function formatDateTime(cellValue: Date) {
    if (isNil(cellValue)) {
        return '';
    }
    return dayjs(cellValue).format('YYYY-MM-DD HH:mm:ss');
};
// 格式化日期
export function formatDate(cellValue: Date) {
    if (isNil(cellValue)) {
        return '';
    }
    return dayjs(cellValue).format('YYYY-MM-DD');
};

/**
 * 删除对象上指定属性
 */
export function removeProperty<T extends object, K extends keyof T>(o: T, pros: K[]): void {
    for (let index = 0; index < pros.length; index++) {
        const key = pros[index];
        if (Reflect.has(o, key)) {
            Reflect.deleteProperty(o, key);
        }
    }
}
/**
 * 删除对象上
 * @param obj 
 */
export function deleteAllProperties<T extends object>(obj: T): void {
    for (const prop in obj) {
        Reflect.deleteProperty(obj, prop);
    }
}

/**
 * 返回指定时间段内的月份数组
 * @param startDate 
 * @param endDate 
 * @returns 
 */
export function getMonthsBetween(startDate: Date, endDate: Date): string[] {
    let months: string[] = [];
    let current = startDate;
    while (current.getTime() <= endDate.getTime()) {
        months.push(dayjs(current).format('YYYYMM'));
        current.setMonth(current.getMonth() + 1);
    }
    return months;
}


export function str2array(
    str: string,
    delimiters: string[]
): string[] {
    // 构造正则表达式，匹配任意分隔符
    const delimiterRegex = new RegExp(`[${delimiters.join('')}]+`, 'g');

    // 按正则表达式拆分字符串
    return str.split(delimiterRegex).filter(Boolean); // 过滤掉空字符串
}